package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.custom.RoundedImageView;
import com.luck.easyfoodbatch.glide.GlideCircleTransform;
import com.luck.easyfoodbatch.mvp.model.EvaluateM;
import com.luck.easyfoodbatch.util.HttpUtil;
import com.luck.easyfoodbatch.util.Tools;
import com.luck.easyfoodbatch.util.XLHRatingBar;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class EvaluationAdapter extends BaseAdapter {
    private Context mContext;
    private List<EvaluateM.DataBean> list = new ArrayList<>();

    public EvaluationAdapter(Context context) {
        this.mContext = context;
    }

    public void setList(List<EvaluateM.DataBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(mContext).inflate(R.layout.evaluation_item, null);
            holder.tv_name = (TextView) view.findViewById(R.id.tv_name);
            holder.tv_time = (TextView) view.findViewById(R.id.tv_time);
            holder.tv_content = (TextView) view.findViewById(R.id.tv_content);
            holder.riv_avatar = (RoundedImageView) view.findViewById(R.id.riv_avatar);
            holder.ratingBar3 = (XLHRatingBar) view.findViewById(R.id.ratingBar3);
            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        EvaluateM.DataBean bean = list.get(i);
        String uname = bean.getUname();
        String avatar_path = bean.getAvatar_path();
        String content = bean.getContent();
        int shop_star = bean.getShop_star();
        int created_at = bean.getCreated_at();
        if (!Tools.isNull(avatar_path)) {
            Glide.with(mContext)
                    .load(HttpUtil.IMG_BASE_URL + avatar_path)
                    .centerCrop()
                    .placeholder(R.mipmap.loading_4b3)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .transform(new GlideCircleTransform(mContext))
                    .into(holder.riv_avatar);
        } else {
            holder.riv_avatar.setImageResource(R.mipmap.loading_4b3);
        }

        if (!Tools.isNull(uname)) {
            holder.tv_name.setText(uname);
        } else {
            holder.tv_name.setText("");
        }
        if (created_at != 0) {
            holder.tv_time.setText(Tools.SubTime2(created_at + ""));
        } else {
            holder.tv_time.setText("");
        }
        holder.ratingBar3.setCountSelected(shop_star);

        if (!Tools.isNull(content)) {
            holder.tv_content.setText(content);
        } else {
            holder.tv_content.setText("");
        }

        return view;
    }

    class Holder {
        RoundedImageView riv_avatar;
        TextView tv_name, tv_time, tv_content;
        XLHRatingBar ratingBar3;
    }

}
